﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using HslCommunication.BasicFramework;
using HslCommunication.LogNet;

namespace HslCommunication.Core.Net
{
	/// <summary>
	/// 基于单次无协议的网络交互的基类，通常是串口协议扩展成网口协议的基类<br />
	/// Base class based on a single non-protocol network interaction, usually the base class that the serial port protocol is extended to the network port protocol
	/// </summary>
	// Token: 0x0200013A RID: 314
	public class NetworkDeviceSoloBase : NetworkDeviceBase
	{
		/// <summary>
		/// 实例化一个默认的对象<br />
		/// Instantiate a default object
		/// </summary>
		// Token: 0x06001B3B RID: 6971 RVA: 0x000878F9 File Offset: 0x00085AF9
		public NetworkDeviceSoloBase()
		{
			base.ReceiveTimeOut = 5000;
			base.SleepTime = 20;
		}

		/// <summary>
		/// 从串口接收一串数据信息，可以指定是否一定要接收到数据<br />
		/// Receive a string of data information from the serial port, you can specify whether you must receive data
		/// </summary>
		/// <param name="socket">串口对象</param>
		/// <param name="awaitData">是否必须要等待数据返回</param>
		/// <returns>结果数据对象</returns>
		// Token: 0x06001B3C RID: 6972 RVA: 0x00087918 File Offset: 0x00085B18
		protected OperateResult<byte[]> ReceiveSolo(Socket socket, bool awaitData)
		{
			bool flag = !Authorization.nzugaydgwadawdibbas();
			OperateResult<byte[]> result;
			if (flag)
			{
				result = new OperateResult<byte[]>(StringResources.Language.AuthorizationFailed);
			}
			else
			{
				byte[] buffer = new byte[1024];
				MemoryStream memoryStream = new MemoryStream();
				HslTimeOut hslTimeOut = HslTimeOut.HandleTimeOutCheck(socket, this.receiveTimeOut);
				try
				{
					Thread.Sleep(base.SleepTime);
					int count = socket.Receive(buffer);
					hslTimeOut.IsSuccessful = true;
					memoryStream.Write(buffer, 0, count);
				}
				catch (Exception ex)
				{
					hslTimeOut.IsSuccessful = true;
					memoryStream.Dispose();
					bool isTimeout = hslTimeOut.IsTimeout;
					if (isTimeout)
					{
						return new OperateResult<byte[]>(StringResources.Language.ReceiveDataTimeout + " " + base.ReceiveTimeOut.ToString() + "ms");
					}
					return new OperateResult<byte[]>(ex.Message);
				}
				byte[] value = memoryStream.ToArray();
				memoryStream.Dispose();
				result = OperateResult.CreateSuccessResult<byte[]>(value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.Net.NetworkDeviceSoloBase.ReceiveSolo(System.Net.Sockets.Socket,System.Boolean)" />
		// Token: 0x06001B3D RID: 6973 RVA: 0x00087A20 File Offset: 0x00085C20
		[DebuggerStepThrough]
		protected Task<OperateResult<byte[]>> ReceiveSoloAsync(Socket socket, bool awaitData)
		{
			NetworkDeviceSoloBase.<ReceiveSoloAsync>d__2 <ReceiveSoloAsync>d__ = new NetworkDeviceSoloBase.<ReceiveSoloAsync>d__2();
			<ReceiveSoloAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReceiveSoloAsync>d__.<>4__this = this;
			<ReceiveSoloAsync>d__.socket = socket;
			<ReceiveSoloAsync>d__.awaitData = awaitData;
			<ReceiveSoloAsync>d__.<>1__state = -1;
			<ReceiveSoloAsync>d__.<>t__builder.Start<NetworkDeviceSoloBase.<ReceiveSoloAsync>d__2>(ref <ReceiveSoloAsync>d__);
			return <ReceiveSoloAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06001B3E RID: 6974 RVA: 0x00087A74 File Offset: 0x00085C74
		public override OperateResult<byte[]> ReadFromCoreServer(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			ILogNet logNet = base.LogNet;
			if (logNet != null)
			{
				logNet.WriteDebug(this.ToString(), StringResources.Language.Send + " : " + SoftBasic.ByteToHexString(send, ' '));
			}
			OperateResult operateResult = base.Send(socket, send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				if (socket != null)
				{
					socket.Close();
				}
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				bool flag2 = this.receiveTimeOut < 0;
				if (flag2)
				{
					result = OperateResult.CreateSuccessResult<byte[]>(new byte[0]);
				}
				else
				{
					OperateResult<byte[]> operateResult2 = this.ReceiveSolo(socket, false);
					bool flag3 = !operateResult2.IsSuccess;
					if (flag3)
					{
						if (socket != null)
						{
							socket.Close();
						}
						result = new OperateResult<byte[]>(operateResult2.Message);
					}
					else
					{
						ILogNet logNet2 = base.LogNet;
						if (logNet2 != null)
						{
							logNet2.WriteDebug(this.ToString(), StringResources.Language.Receive + " : " + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
						}
						result = OperateResult.CreateSuccessResult<byte[]>(operateResult2.Content);
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06001B3F RID: 6975 RVA: 0x00087B80 File Offset: 0x00085D80
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadFromCoreServerAsync(Socket socket, byte[] send, bool hasResponseData = true, bool usePackHeader = true)
		{
			NetworkDeviceSoloBase.<ReadFromCoreServerAsync>d__4 <ReadFromCoreServerAsync>d__ = new NetworkDeviceSoloBase.<ReadFromCoreServerAsync>d__4();
			<ReadFromCoreServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromCoreServerAsync>d__.<>4__this = this;
			<ReadFromCoreServerAsync>d__.socket = socket;
			<ReadFromCoreServerAsync>d__.send = send;
			<ReadFromCoreServerAsync>d__.hasResponseData = hasResponseData;
			<ReadFromCoreServerAsync>d__.usePackHeader = usePackHeader;
			<ReadFromCoreServerAsync>d__.<>1__state = -1;
			<ReadFromCoreServerAsync>d__.<>t__builder.Start<NetworkDeviceSoloBase.<ReadFromCoreServerAsync>d__4>(ref <ReadFromCoreServerAsync>d__);
			return <ReadFromCoreServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06001B40 RID: 6976 RVA: 0x00087BE1 File Offset: 0x00085DE1
		public override string ToString()
		{
			return string.Format("NetworkDeviceSoloBase<{0}>[{1}:{2}]", base.ByteTransform.GetType(), this.IpAddress, this.Port);
		}
	}
}
